package yingke.com.yingke.ui.left.nearby;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import yingke.com.yingke.R;
import yingke.com.yingke.config.Constant;
import yingke.com.yingke.config.Live;
import yingke.com.yingke.ui.right.utils.OkHttpClientUtils;

/**
 * 放附近的内容的Fragment
 */
public class NearbyFragment extends Fragment {


    private XRecyclerView xcl;
    private NearXRecyclerAdapter adapter;
    private List<Live> lives;
    private Handler mHandler =new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what ==1){
                lives = (List<Live>) msg.obj;
                adapter.setData(lives);


            }
        }
    };


    public NearbyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_nearby, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        xcl = (XRecyclerView) view.findViewById(R.id.xcl_left_nearby);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),3);
        xcl.setLayoutManager(layoutManager);
        adapter = new NearXRecyclerAdapter(getContext());
        xcl.setAdapter(adapter);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonData = OkHttpClientUtils.getStringFromUrl(Constant.RECOMMEND_PATH);
                    JSONObject object = new JSONObject(jsonData);
                    JSONArray livesArray = object.getJSONArray("lives");
                    Gson gson = new Gson();
                    String array=livesArray.toString();
                    Type type = new TypeToken<List<Live>>(){}.getType();
                    List<Live> lives = gson.fromJson(array, type);
                    Message msg = Message.obtain();
                    msg.what =1;
                    msg.obj = lives;
                    mHandler.sendMessage(msg);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }
}
